/**
 * 
 */
package kr.co.insoft.auth.service.members.impl;

import java.util.ArrayList;
import java.util.List;

import kr.co.insoft.auth.model.mail.MaillingFormModel;
import kr.co.insoft.auth.model.members.JoinFormModel;
import kr.co.insoft.auth.model.organization.OrganizationEntityModel;
import kr.co.insoft.auth.model.organization.OrganizationUserEntityModel;
import kr.co.insoft.auth.repository.auth.AuthoritiesRepository;
import kr.co.insoft.auth.repository.mail.MaillingRepository;
import kr.co.insoft.auth.repository.members.JoinRepository;
import kr.co.insoft.auth.repository.organization.OrganizationRepository;
import kr.co.insoft.auth.repository.organization.OrganizationUserRepository;
import kr.co.insoft.auth.service.members.JoinService;
import kr.co.insoft.framework.config.AuthUtil;
import kr.co.insoft.framework.config.RoleTypes;
import kr.co.insoft.framework.exceptions.ExceptionCode;
import kr.co.insoft.framework.exceptions.RepositoryException;
import kr.co.insoft.framework.exceptions.ServiceException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author IN-SOFT Inc. GoodwillDD(sylee@in-soft.co.kr)
 * 
 */
@Service
public class JoinServiceImpl implements JoinService {

	@Autowired
	JoinRepository joinRepository;

	@Override
	public boolean isDuplicateUsername(String username) throws ServiceException {
		try {
			return joinRepository.isDuplicateUsername(username);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION);
		}
	}

	@Autowired
	MaillingRepository maillingRepository;

	@Autowired
	AuthoritiesRepository authoritiesRepository;

	@Autowired
	OrganizationUserRepository organizationUserRepository;

	@Autowired
	OrganizationRepository organizationRepository;

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void registMember(JoinFormModel joinFormModel)
			throws ServiceException {
		try {
			joinRepository.registMember(joinFormModel);
			/* 일반 회원 / 기업회원에 따라서 권한을 다르게 추가한다. */
			List<String> auths = new ArrayList<String>();
			if (joinFormModel.isCompany()) {
				/*
				 * 기업 회원은 초기에 아무런 액션도 할 수 없다.
				 * 
				 * 관리자가 해당 기업을 생성하고 관리자를 지정한 이후에 권한을 부여받아야만 사용할 수 있다.
				 */
				auths.add(RoleTypes.ROLE_CUSER.name());
				auths.addAll(AuthUtil.getInitCompanyUserAuth());
				joinFormModel.setAuths(auths);
			} else {
				OrganizationEntityModel organizationEntityModel = new OrganizationEntityModel();
				organizationEntityModel.setOrgName(joinFormModel.getNickname());
				organizationRepository
						.createOrganization(organizationEntityModel);

				OrganizationUserEntityModel organizationUserEntityModel = new OrganizationUserEntityModel();
				organizationUserEntityModel.setOrgid(organizationEntityModel
						.getOrgid());
				organizationUserEntityModel.setUserid(joinFormModel.getId());
				organizationUserRepository
						.createOrganizationUser(organizationUserEntityModel);

				auths.add(RoleTypes.ROLE_NUSER.name());
				auths.addAll(AuthUtil.getInitNormalUserAuth());
				joinFormModel.setAuths(auths);
			}

			authoritiesRepository.addUserMenuAuth(joinFormModel);
			maillingRepository.sendRegistedMail(joinFormModel);
		} catch (RepositoryException e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION, e);
		}
	}

	@Override
	@Transactional(rollbackFor = ServiceException.class)
	public void accountRevitalize(String confirmKey) throws ServiceException {
		try {
			MaillingFormModel mail = maillingRepository.getMail(confirmKey);
			joinRepository.accountRevitalize(mail.getUserid());
			maillingRepository.cleanMail(mail.getSeq());
		} catch (Exception e) {
			throw new ServiceException(ExceptionCode.SERVICE_EXCEPTION);
		}
	}
}
